package com.fesine.hibernate.service;

import com.fesine.hibernate.model.*;
import com.fesine.hibernate.model.Class;
import com.fesine.hibernate.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.*;

/**
 * Created by Fesine on 2016/5/22.
 */
public class TeacherTest2 {

    private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
    private Session session;


    @Before
    public void setUp() throws Exception {
        // 4、创建session
        session = sessionFactory.openSession();
        // 5、开启事务
        session.beginTransaction();
    }

    @After
    public void tearDown() throws Exception {
        // 提交事务
        session.getTransaction().commit();
        session.close();
    }

    @Test
    public void testSave() throws Exception {
        com.fesine.hibernate.model.Class c = new Class();
        c.setName("04计算机系");
        Student student = new Student();
        student.setName("pactera");
        student.setC(c);
        Student student1 = new Student();
        student1.setName("david");
        student1.setC(c);
        //session.save(c);//没有先插入班级信息，而是在插入学生信息时同步完成插入班级信息
        session.save(student);
        session.save(student1);
    }
}
